﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace winpacklib
{
    public class RepositoryManager
    {
        private List<Repository> _repositories = new List<Repository>();

        /// <summary>
        /// List of all repositories in this manager
        /// </summary>
        public List<Repository> Repositories
        {
            get { return _repositories; }
            set { _repositories = value; }
        }

        /// <summary>
        /// List of all global packages
        /// </summary>
        private Dictionary<String, Package> _packages = new Dictionary<String, Package>();

        public Dictionary<String, Package> Packages
        {
            get { return _packages; }
            set { _packages = value; }
        }

        /// <summary>
        /// Add repository to manager with url
        /// </summary>
        /// <param name="url"></param>
        public void AddRepository(String url)
        {
            Repository rep = new Repository(url);
            _repositories.Add(rep);
        }

        /// <summary>
        /// Update all registered repositories
        /// </summary>
        public void UpdateRepositories()
        {
            foreach (Repository r in _repositories)
            {
                r.Update();
                foreach (ServerPackage p in r.Packages)
                {
                    if (_packages.ContainsKey(p.ToString()))
                    {
                        _packages[p.ToString()].Repositories.Add(r);
                    }
                    else
                    {
                        //Change ServerPackage to Package
                        Package newPackage = (Package)p;
                        newPackage.Repositories.Add(r);

                        _packages.Add(newPackage.ToString(), newPackage);
                    }

                }
            }
        }

    }
}
